package com.newbee.menu;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

public class SlideMenu extends HorizontalScrollView
{

	private LinearLayout mWapper;
	private ViewGroup mMenu;
	private ViewGroup mContent;
	private int mScreenWith;
	private int mMenuLeftPadding;
	private boolean once = false;

	private int mMenuWidth;

	private boolean isOpen;

	/**
	 * 未使用自定义属性时
	 * 
	 * @param context
	 * @param attrs
	 */
	public SlideMenu(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		WindowManager windowManager = (WindowManager) context
				.getSystemService(context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		windowManager.getDefaultDisplay().getMetrics(outMetrics);
		mScreenWith = outMetrics.widthPixels;
		/**
		 * 左边边留50dp
		 */
		// mMenuLeftPadding = (int)
		// TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80,
		// context.getResources().getDisplayMetrics());
		mMenuWidth = (int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, 80, context.getResources()
						.getDisplayMetrics());
	}

	/**
	 * 设置子view的宽和高
	 */
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
	{
		if (once == false)
		{
			mWapper = (LinearLayout) getChildAt(0);
			mContent = (ViewGroup) mWapper.getChildAt(0);
			mMenu = (ViewGroup) mWapper.getChildAt(1);
			// mMenuWidth = mMenu.getLayoutParams().width = mScreenWith -
			// mMenuLeftPadding;
			mMenu.getLayoutParams().width = mMenuWidth;
			mContent.getLayoutParams().width = mScreenWith;
			once = true;
		}

		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	/**
	 * 通过设置偏移量来实现开始内容的展示
	 * 
	 */
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b)
	{
		super.onLayout(changed, l, t, r, b);
		if (changed)
		{
			this.scrollTo(0, 0);
		}

	}

	@Override
	public boolean onTouchEvent(MotionEvent ev)
	{

		int action = ev.getAction();
		switch (action)
		{
		case MotionEvent.ACTION_UP:
			int scrollX = getScrollX();
			if (scrollX <= mMenuWidth / 2)
			{

				this.smoothScrollTo(0, 0);
				this.isOpen = false;
			}
			else
			{
				this.smoothScrollTo(mMenuWidth, 0);
				this.isOpen = true;
			}
			return true;
		default:
			break;
		}

		return super.onTouchEvent(ev);
	}

	public void openMenu()
	{
		if (isOpen)
		{
			return;
		}
		else
		{
			this.smoothScrollTo(mMenuWidth, 0);
			isOpen = true;
		}
	}

	public void closeMenu()
	{
		if (isOpen)
		{

			this.smoothScrollTo(0, 0);
			isOpen = false;
		}
		else
		{

			return;
		}
	}

	public void toggle()
	{
		if (isOpen)
		{
			closeMenu();
		}
		else
		{
			openMenu();
		}
	}

}
